package com.kakavr.staff.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.auth.util.EncryptionUtils;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.staff.service.IStaffAdminService;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
import com.kakavr.work.service.IWorkRecordService;
/**
 * 员工
 * @author eli.zhu
 *
 */
@RestController
@RequestMapping("staff")
public class StaffController extends BaseController {
	@Resource
	private IStaffAdminService staffAdminService;
	@Resource
	private IWorkRecordService workRecordService;

	/**
	 * 修改个人密码
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updatePass", method = RequestMethod.POST)
	public Map<String, Object> updatePass(HttpServletRequest request) {
		String oldPass = request.getParameter("oldPass");
		String password = request.getParameter("password");
		if (StringUtils.isBlank(oldPass)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "原密码");
		}
		if (StringUtils.isBlank(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1001,
					MsgConstants.MSG_CONTENT_1001, "新密码");
		}
		if(!ValidatorUtils.isPass(password)){
			throw new ValidateParamException(MsgConstants.MSG_CODE_2004,
					MsgConstants.MSG_CONTENT_2004, "新密码");
		}
		if (oldPass.equals(password)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_2003,
					MsgConstants.MSG_CONTENT_2003);
		}
		String pass = SessionUtils.getPass();
		if (oldPass.equals(pass) || EncryptionUtils.md5(oldPass).equals(pass)) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", SessionUtils.getUserId());
			map.put("password", EncryptionUtils.md5(password));
			staffAdminService.updateStaff(map);
			return MsgResult.okResult();
		}
		throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
				MsgConstants.MSG_CONTENT_1002, "原密码");
	}
	
	/**
	 * 统计提成
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "staticOwnCommis", method = RequestMethod.GET)
	public Map<String, Object> staticOwnCommis(HttpServletRequest request){
		Map<String, Object> map = Maps.newHashMap();
		map.put("begin", request.getParameter("begin"));
		map.put("end", request.getParameter("end"));
		map.put("type", request.getParameter("type"));
		map.put("adminId", SessionUtils.getUserId());
		return MsgResult.okResultData(workRecordService.staticWorkByOwn(map));
	}

}
